from typing import List

class Solution:
    def canFinish(self, numCourses: int, prerequisites: List[List[int]]) -> bool:
        # 构造maps
        maps = {i: [] for i in range(numCourses)}
        for a, b in prerequisites:
            maps[a].append(b)
        
        while True:
            can_learn = [k for k, v in maps.items() if len(v) == 0]
            if len(can_learn) == 0:
                return False
            for i in can_learn:
                del maps[i]
            if len(maps) == 0:
                return True
            for k in maps:
                for i in can_learn:
                    if i in maps[k]:
                        maps[k].remove(i)
        return False


if __name__ == "__main__":
    numCourses = 2
    prerequisites = [[1,0],[0,1]]
    Solution().canFinish(numCourses, prerequisites)
